package com.ruoyi.web.controller.crm;

import com.bonuo.crm.domain.Agent;
import com.bonuo.crm.domain.Client;
import com.bonuo.crm.service.ClientService;
import com.bonuo.crm.service.FacultyService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

//客户controller
@RestController
@RequestMapping("/crm/client")
@Api(tags = "客户管理")
public class ClientController extends BaseController {
    @Autowired
    private ClientService clientService;
    @Autowired
    private FacultyService facultyService;

    //查询客户信息
    @GetMapping("/list")
    @ApiOperation(value = "查询客户信息",httpMethod = "GET")
    public TableDataInfo selectClientList(Client client,Integer pageNum,Integer pageSize){
        if(pageNum!=null && pageSize!=null){
            startPage();
        }
        List<Client> clientList = clientService.selectClientList(client);
        return getDataTable(clientList);
    }

    //查询博诺集团客户
    @ApiOperation(value = "查询博诺集团客户",httpMethod = "GET")
    @GetMapping("/selectClientBNList")
    public TableDataInfo selectClientBNList(Client client,Integer pageNum,Integer pageSize){
        if(pageNum!=null && pageSize!=null){
            startPage();
        }
        List<Client> clientList = clientService.selectClientBNList(client);
        return getDataTable(clientList);
    }

    //添加客户信息
    @ApiOperation(value = "添加客户信息",httpMethod = "POST")
    @RequestMapping("/add")
    public AjaxResult add(@RequestBody Client client){
        //查询客户是否存在根据税号
        String selectClientByTaxNumber = clientService.selectClientByTaxNumber(client);
        if(selectClientByTaxNumber != null && !selectClientByTaxNumber.equals("")){
            return AjaxResult.error("统一社会信用代码已存在");
        }
        Integer add = clientService.insertClient(client);
        return toAjax(add);
    }

    //删除客户信息
    @ApiOperation(value = "删除客户信息",httpMethod = "DELETE")
    @RequestMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Integer id){
        //查询这个客户下是否有相关院系
        Integer count = facultyService.selectFacultyByClientId(id);
        if(count >0) {
            return error("该客户下含有院系信息，请删除所有院系信息重试");
        }
        Integer delete = clientService.deleteClientById(id);
        return toAjax(delete);
    }

    //修改客户信息
    @ApiOperation(value = "修改客户信息",httpMethod = "PUT")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody Client client){
        //查询客户是否存在根据税号
        String selectClientByTaxNumber = clientService.selectClientByTaxNumber(client);
        if(selectClientByTaxNumber != null && !selectClientByTaxNumber.equals("")){
            return AjaxResult.error("统一社会信用代码已存在");
        }
        Integer update = clientService.updateClient(client);
        return toAjax(update);
    }

    //客户导出
    @ApiOperation(value = "导出客户信息",httpMethod = "POST")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Client client) {
        List<Client> list = clientService.selectClientList(client);
        ExcelUtil<Client> util = new ExcelUtil<Client>(Client.class);
        util.exportExcel(response, list, "客户信息数据");
    }
}
